<template>
  <a-modal
    title="修改字典项"
    :width="640"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          v-show="!addable"
          label="id"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            v-decorator="['id']"
            :disabled="editable || showable"
          />
        </a-form-item>
        <a-form-item
          label="字典名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            :disabled="showable"
            v-decorator="['itemText', {rules: [{required: true, message: '请输入字典名称'}]}]" />
        </a-form-item>
        <a-form-item
          label="字典代码"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            :disabled="showable"
            v-decorator="['itemCode', {rules: [{required: true, message: '请输入字典代码'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>
    <template slot="footer">
      <a-button key="cancel" @click="handleCancel">取消</a-button>
      <a-button key="forward" :loading="confirmLoading" type="primary" @click="handleOk">保存</a-button>
    </template>
  </a-modal>
</template>

<script>
import { getItemObj, addItemObj, putItemObj } from '@api/sys/dict'
import pick from 'lodash.pick'
export default {
  name: 'DictItemAddOrUpdate',
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      },
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
      type: 'add',
      editable: false,
      addable: false,
      showable: false,
      dictId: ''
    }
  },
  methods: {
    // 获取用户信息
    edit (id, dictId, type) {
      this.visible = true
      this.dictId = dictId
      if (type && type === 'add') {
        this.addable = true
        this.type = type
      }
      if (type === 'edit') {
        this.editable = true
        this.type = type
      }
      if (type === 'show') {
        this.showable = true
        this.type = type
      }
      const { form: { setFieldsValue, resetFields } } = this
      if (['edit', 'show'].includes(type)) {
        this.confirmLoading = true
        getItemObj(id).then(res => {
          const record = res.data
          this.confirmLoading = false
          this.$nextTick(() => {
            setFieldsValue(pick(record, ['id', 'itemText', 'itemCode']))
          })
        })
      } else {
        resetFields()
      }
    },
    selectChange (e) {
      this.dsType = e
    },
    handleOk () {
      this.form.validateFields(async (err, values) => {
        if (!err) {
          values.dictId = this.dictId
          if (this.type === 'add') {
            await addItemObj(values)
          } else if (this.type === 'edit') {
            await putItemObj(values)
          }
          this.confirmLoading = true
          setTimeout(() => {
            this.confirmLoading = false
            this.$emit('ok')
            this.visible = false
          }, 1500)
        }
      })
    },
    handleCancel () {
      this.visible = false
      setTimeout(() => {
        this.addable = false
        this.showable = false
        this.editable = false
      }, 200)
    }
  }
}
</script>

<style scoped>

</style>
